const { Menu, Role, User } = require("../models/role");
const { Good, Sku, Tag } = require("../models/good");
const { Address, AppUser } = require("../models/appUser");
const { Order } = require("../models/order");
const { Comment } = require("../models/comment");

async function insertUser() {
  // 先删除所有数据
  await Menu.destroy({ where: {} });
  await Role.destroy({ where: {} });
  await User.destroy({ where: {} });
  // 创建menu
  const menu1 = await Menu.create({
    name: "首页",
    menu_code: "home",
    nodeType: 1,
    level: 1,
    icon: "el-icon-s-home",
    link_url: "/home",
  });

  // 创建子菜单 仪表盘
  const menu2 = await Menu.create({
    name: "仪表盘",
    menu_code: "dashboard",
    nodeType: 1,
    level: 2,
    icon: "el-icon-s-data",
    link_url: "/dashboard",
    parent_id: menu1.id,
  });
  // 创建menu 商品
  const menu3 = await Menu.create({
    name: "商品",
    menu_code: "product",
    nodeType: 1,
    level: 1,
    icon: "el-icon-s-goods",
    link_url: "/product",
  });
  const menu12 = await Menu.create({
    name: "分类管理",
    menu_code: "productTag",
    nodeType: 1,
    level: 2,
    icon: "el-icon-s-goods",
    link_url: "/productTag",
    parent_id: menu3.id,
  });

  // 创建子菜单 商品列表
  const menu4 = await Menu.create({
    name: "商品列表",
    menu_code: "productList",
    nodeType: 1,
    level: 2,
    icon: "el-icon-s-order",
    link_url: "/productList",
    parent_id: menu3.id,
  });
  // 创建子菜单 添加商品
  const menu5 = await Menu.create({
    name: "添加商品",
    menu_code: "addProduct",
    nodeType: 1,
    level: 2,
    icon: "el-icon-circle-plus-outline",
    link_url: "/addProduct",
    parent_id: menu3.id,
  });
  // 创建menu 订单
  const menu6 = await Menu.create({
    name: "订单",
    menu_code: "order",
    nodeType: 1,
    level: 1,
    icon: "el-icon-s-order",
    link_url: "/order",
  });
  // 创建子菜单 订单列表
  const menu7 = await Menu.create({
    name: "订单列表",
    menu_code: "orderList",
    nodeType: 1,
    level: 2,
    icon: "el-icon-s-order",
    link_url: "/orderList",
    parent_id: menu6.id,
  });
  // 创建menu 权限
  const menu8 = await Menu.create({
    name: "权限",
    menu_code: "permission",
    nodeType: 1,
    level: 1,
    icon: "el-icon-lock",
    link_url: "/permission",
  });
  // 创建子菜单 角色管理
  const menu9 = await Menu.create({
    name: "角色管理",
    menu_code: "role",
    nodeType: 1,
    level: 2,
    icon: "el-icon-s-custom",
    link_url: "/role",
    parent_id: menu8.id,
  });
  // 创建子菜单 菜单管理
  const menu10 = await Menu.create({
    name: "菜单管理",
    menu_code: "menu",
    nodeType: 1,
    level: 2,
    icon: "el-icon-menu",
    link_url: "/menu",
    parent_id: menu8.id,
  });
  // 创建子菜单 用户管理
  const menu11 = await Menu.create({
    name: "用户管理",
    menu_code: "user",
    nodeType: 1,
    level: 2,
    icon: "el-icon-user",
    link_url: "/user",
    parent_id: menu8.id,
  });
  // 插入所有菜单
  const role = await Role.create({
    name: "超级管理员",
    desc: "超级管理员",
  });
  await role.addMenus([
    menu1,
    menu2,
    menu3,
    menu12,
    menu4,
    menu5,
    menu6,
    menu7,
    menu8,
    menu9,
    menu10,
    menu11,
  ]);
  // 创建用户
  const user = await User.create({
    username: "admin",
    password: "admin",
    name: "admin",
    introduction: "超级管理员",
    avatar: "http://127.0.0.1:8123/public/avatar/1736394892551.png",
  });
  // 为用户分配超级管理员角色
  await role.addUsers(user);

  console.log("数据插入成功");
}

// 上传商品
async function inserGood() {
  await Tag.destroy({ where: {} });
  await Sku.destroy({ where: {} });
  await Good.destroy({ where: {} });
  // 先添加分类
  const tag1 = await Tag.create({
    name: "学习书刊",
  });
  const tag2 = await Tag.create({
    name: "小说书刊",
  });
  const tag3 = await Tag.create({
    name: "时尚杂志",
  });
  // 儿童期刊 明星传记 励志书刊
  const tag4 = await Tag.create({
    name: "儿童期刊",
  });
  const tag5 = await Tag.create({
    name: "明星传记",
  });
  const tag6 = await Tag.create({
    name: "励志书刊",
  });
  // 添加图书 数据结构与算法 计算机网络 操作系统 数据库
  // 数据结构与算法 入门到提高 python语言实现
  const good1 = await Good.create({
    name: "数据结构与算法",
    introduction: "数据结构与算法",
    price: 50,
    stock_quantity: 100,
    avatar: "http://127.0.0.1:8123/public/images/01.jpg",
    tagId: tag1.id,
    author: "张三",
    skuList: [
      {
        name: "入门到提高",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "python语言实现",
        amount: 50,
        stock_quantity: 100,
      },
    ],
    isRecommend: true,
    imageList: JSON.stringify(["http://127.0.0.1:8123/public/images/01.jpg"]),
  });
  // 计算机网络第七版 计算机网络第六版 计算机网络第五版
  const good2 = await Good.create({
    name: "计算机网络",
    introduction: "计算机网络",
    price: 50,
    stock_quantity: 100,
    avatar: "http://127.0.0.1:8123/public/images/02.jpg",
    tagId: tag1.id,
    author: "张三",
    skuList: [
      {
        name: "第七版",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "第六版",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "第五版",
        amount: 50,
        stock_quantity: 100,
      },
    ],
    isRecommend: true,
    imageList: JSON.stringify(["http://127.0.0.1:8123/public/images/02.jpg"]),
  });

  // 计算机操作系统 西安电子科技大学出版 计算机操作系统第五版
  const good3 = await Good.create({
    name: "计算机操作系统",
    introduction: "计算机操作系统",
    price: 50,
    stock_quantity: 100,
    author: "张三",
    avatar: "http://127.0.0.1:8123/public/images/03.jpg",
    tagId: tag1.id,
    skuList: [
      {
        name: "西安电子科技大学出版",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "第五版",
        amount: 50,
        stock_quantity: 100,
      },
    ],
    isRecommend: true,
    imageList: JSON.stringify(["http://127.0.0.1:8123/public/images/03.jpg"]),
  });
  // 数据库基础与实训教程 sql server数据库 mysql数据库
  const good4 = await Good.create({
    name: "数据库",
    introduction: "数据库",
    price: 50,
    stock_quantity: 100,
    author: "张三",
    avatar: "http://127.0.0.1:8123/public/images/04.jpg",
    tagId: tag1.id,
    skuList: [
      {
        name: "基础与实训教程",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "sql server数据库",
        amount: 50,
        stock_quantity: 100,
      },
      {
        name: "mysql数据库",
        amount: 50,
        stock_quantity: 100,
      },
    ],
    isRecommend: true,
    imageList: JSON.stringify(["http://127.0.0.1:8123/public/images/03.jpg"]),
  });
}

insertUser();
inserGood();
